package com.metagurukul.metagranth.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.metagurukul.metagranth.model.common.connection.ConnectionPoolFactory;
import com.metagurukul.metagranth.vo.Question;
import com.metagurukul.metagranth.vo.Tag;

/**
 * Servlet implementation class ControllerServlet
 */
public class ControllerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ControllerServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	/**
	 * init() method is for first time controllerServlet start then initialize
	 * the Connection pool
	 */
	public void init() throws ServletException {
		ConnectionPoolFactory cp = ConnectionPoolFactory.getInstance();
	}

	/**
	 * destroy() for releasing all the connection that are in Connection Pool
	 */
	public void destroy() {
		ConnectionPoolFactory cp = ConnectionPoolFactory.getInstance();
		cp.release();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	private void doPerform(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String path = request.getServletPath();
		path = path.substring(1, path.length() - 3);
		RequestDispatcher rd = null;
		if ("login".equals(path)) {
			rd = request.getRequestDispatcher("LoginServlet");
			rd.forward(request, response);
		} else if ("askquestion".equals(path)) {
			rd = request.getRequestDispatcher("AskQuestionServlet");
			rd.forward(request, response);
		} else if ("tags".equals(path)) {
			rd = request.getRequestDispatcher("GetTagListServlet");
			rd.forward(request, response);
		} else if ("logout".equals(path)) {
			rd = request.getRequestDispatcher("LogoutServlet");
			rd.forward(request, response);
		} else if ("questions".equals(path)) {
			rd = request.getRequestDispatcher("GetLatestQuestionServlet");
			rd.forward(request, response);
		} else if ("question-discription".equals(path)) {
			rd = request.getRequestDispatcher("GetQuestionDetailServlet");
			rd.forward(request, response);
		} else if ("allquestions".equals(path)) {
			rd = request.getRequestDispatcher("AllQuestionListServlet");
			rd.forward(request, response);
		} else if ("searchQuestion".equals(path)) {
			rd = request.getRequestDispatcher("SearchQuestionServlet");
			rd.forward(request, response);
		} else if ("signup".equals(path)) {
			// transfer request to RegistrationServlet
			rd = request.getRequestDispatcher("RegistrationServlet");
			rd.forward(request, response);
		} else if ("ChangePassword".equals(path)) {
			// transfer request to changePasswordServlet
			rd = request.getRequestDispatcher("ChangePasswordServlet");
			rd.forward(request, response);
		} else if ("admin-approve-answer".equals(path)) {
			rd = request.getRequestDispatcher("AdminAnswerServlet");
			rd.forward(request, response);
		} else if ("adminquestion".equals(path)) {
			rd = request.getRequestDispatcher("AdminServlet");
			rd.forward(request, response);
		} else if ("closequestion".equals(path)) {
			rd = request.getRequestDispatcher("AdminApproveAnswerServlet");
			rd.forward(request, response);
		} else if ("postanswer".equals(path)) {
			rd = request.getRequestDispatcher("PostAnswerServlet");
			rd.forward(request, response);
		} else if ("editQuestions".equals(path)) {
			rd = request.getRequestDispatcher("EditQuestionsServlet");
			rd.forward(request, response);
		} else if ("editableQuestionsDisplay".equals(path)) {
			rd = request.getRequestDispatcher("GetQuestionToBeUpdatedServlet");
			rd.forward(request, response);
		} else if ("editingQuestion".equals(path)) {
			rd = request.getRequestDispatcher("UpdateEditedQuestion");
			rd.forward(request, response);
		} else if ("unansweredquestion".equals(path)) {
			rd = request.getRequestDispatcher("GetUnansweredQuestionServlet");
			rd.forward(request, response);
		}else if ("searchQuestionByTag".equals(path)) {
			rd = request.getRequestDispatcher("SearchQuestionByTagServlet");
			rd.forward(request, response);
		} else if ("profile".equals(path)) {
			rd = request.getRequestDispatcher("ProfileServlet");
			rd.forward(request, response);
		}
	}
}
